<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/","scheme":"Pisces","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="多线程">
<meta property="og:type" content="article">
<meta property="og:title" content="Java日拱一卒-day11">
<meta property="og:url" content="http://yoursite.com/2020/04/24/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day11/index.html">
<meta property="og:site_name" content="很惭愧，就做了一点微小的工作">
<meta property="og:description" content="多线程">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-04-24T02:20:10.000Z">
<meta property="article:modified_time" content="2020-05-17T18:18:40.947Z">
<meta property="article:author" content="文予">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://yoursite.com/2020/04/24/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day11/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Java日拱一卒-day11 | 很惭愧，就做了一点微小的工作</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">很惭愧，就做了一点微小的工作</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">念念不忘，必有回响</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/04/24/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day11/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/uploads/wenyu.png">
      <meta itemprop="name" content="文予">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="很惭愧，就做了一点微小的工作">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Java日拱一卒-day11
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-04-24 10:20:10" itemprop="dateCreated datePublished" datetime="2020-04-24T10:20:10+08:00">2020-04-24</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-05-18 02:18:40" itemprop="dateModified" datetime="2020-05-18T02:18:40+08:00">2020-05-18</time>
              </span>

          
            <div class="post-description">多线程</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="线程简介"><a href="#线程简介" class="headerlink" title="线程简介"></a>线程简介</h2><p>一个进程可以有多个线程，如视频中看视频，听声音，看弹幕等等。</p>
<ul>
<li>程序是指令与数据的有序集合，本身没有运行的含义，是静态概念。</li>
<li>进程是执行程序的一次执行过程，是一个动态概念。是系统资源分配的单位</li>
<li>通常在一个进程中可以包含多喝线程，当然一个进程至少有一个线程</li>
<li>很多多线程都是模拟出来的，实际上在同一个时间点，CPU同时只能执行一个代码，因为切换的很快，所以就有了同时执行的错觉<br>核心概念：</li>
<li>线程就是独立的执行路径</li>
<li>在程序运行时，即使没有自己创建线程，后台也会有多个线程，比如主线程，gc线程；</li>
<li><code>main()</code>称为主线程，是系统的入口，用于执行整个程序。</li>
<li>在一个进程中，如果开辟了多个线程，线程的运行由调度器安排调度，调度器是与操作系统紧密相关的，先后顺序是不能人为干预的。</li>
<li>对同一份资源操作时，会存在资源抢夺的问题，需要加入并发控制。</li>
<li>线程会带来额外的开销，比如CPU的调度时间，并发控制开销。</li>
<li>每个线程在自己工作内存交互，内存控制不当会造成数据不一致。</li>
</ul>
<h2 id="线程创建"><a href="#线程创建" class="headerlink" title="线程创建"></a>线程创建</h2><p>三种创建方式</p>
<ul>
<li><code>Thread Class</code>：继承<code>Thread</code>类</li>
<li><code>Runnable</code>接口：实现<code>Runnable</code>接口</li>
<li><code>Callable</code>接口：实现<code>Callable</code>接口<br>Java中的线程是作为<code>java.lang.Thread</code>实例出现的。<code>Thread</code>类中定义了管理线程的方法，包括创建、启动、暂停线程等等。常用方法如下。</li>
<li><code>void run()</code>：该方法需要重写，代表了线程的执行体</li>
<li><code>void start()</code>：启动调用该方法的线程（由JVM调用该线程的<code>run()</code>方法）</li>
<li><code>static Thread currentThread()</code>：静态方法，返回当前正在运行的线程对象的引用。</li>
<li><code>final StringgetName()</code>：获取线程名称。</li>
<li><code>static void sleep (long millis) throws InterruptedException</code>：让当前正在运行的线程休眠指定毫秒数。</li>
<li><code>static void yield()</code>：暂停当前正在运行的线程，让出CPU资源。</li>
<li><code>final intgetPriority</code>：获取线程优先级。</li>
<li><code>final void join() throws InterruptedException</code>：令当前进程“加入到”调用<code>join()</code>方法的线程的尾部。</li>
</ul>
<h2 id="继承Thread类创建线程"><a href="#继承Thread类创建线程" class="headerlink" title="继承Thread类创建线程"></a>继承<code>Thread</code>类创建线程</h2><p>步骤如下</p>
<ol>
<li>定义<code>Thread</code>的子类，并且重写该类的<code>run()</code>方法。<code>run()</code>方法的方法体代表了线程需要完成的任务。因此把<code>run()</code>方法称为线程执行体</li>
<li>创建线程对象</li>
<li>线程对象调用<code>start()</code>方法启动该线程<br>示例如下</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">FirstThread</span> <span class="keyword">extends</span> <span class="title">Thread</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> i;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">100</span>; i++) &#123;</span><br><span class="line">            <span class="comment">//当线程类继承Thread类时，直接使用this即可获取当前线程</span></span><br><span class="line">            <span class="comment">//Thread对象的getName()返回当前线程的名字</span></span><br><span class="line">            <span class="comment">//因此可以直接调用getName()返回当前线程的名字</span></span><br><span class="line">            System.out.println(getName()+<span class="string">" "</span>+i);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt;<span class="number">100</span>; i++) &#123;</span><br><span class="line">            <span class="comment">//调用Thread的currentThread()方法获取当前线程，再用getName()获取线程名字</span></span><br><span class="line">            System.out.println(Thread.currentThread().getName()+<span class="string">" "</span>+i);</span><br><span class="line">            <span class="keyword">if</span>(i ==<span class="number">20</span>) &#123;</span><br><span class="line">                <span class="comment">//创建并启动第一个线程</span></span><br><span class="line">                <span class="keyword">new</span> FirstThread().start();</span><br><span class="line">                <span class="comment">//启动第二个</span></span><br><span class="line">                <span class="keyword">new</span> FirstThread().start();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>以上程序的<code>FirstThread</code>类继承了<code>Thread</code>类，实现了<code>run()</code>方法。程序的主方法里也包含了一个循环，当循环变量<code>i</code>等于20时创建两个新线程。虽然以上程序只显式的创建并启动了2个线程，但实际上，包括主线程在内，程序有3个线程。主线程的执行体不是由<code>run()</code>方法确定的，而是由<code>main()</code>方法确定的，<code>main()</code>方法的方法体代表主线程的线程执行体。<br>以上代码还使用了线程的如下两个方法</p>
<ul>
<li><code>Thread.currentThread()</code>：<code>currentThread()</code>是<code>Thread</code>类的静态方法，该方法返回正在执行的线程对象。</li>
<li><code>getName()</code>：该方法是<code>Thread</code>的实例方法，返回调用该方法的线程名字。<br>实际上可以通过<code>setName(String name)</code>方法为线程设置名字，也可以用<code>getName()</code>返回指定线程名字。在默认情况下，主线程名称为<code>main</code>，用户启动的多个线程名字一次为<code>Thread-0</code>、<code>Thread-1</code>、<code>Thread-2</code>、……、<code>Thread-n</code>等等。<br>部分运行结果</li>
</ul>
<blockquote>
<p>Thread-0 41</p>
<p>Thread-1 29</p>
<p>main 78</p>
<p>Thread-1 30</p>
<p>Thread-0 42</p>
<p>Thread-1 31</p>
<p>main 79</p>
<p>Thread-1 32</p>
<p>Thread-0 43</p>
</blockquote>
<p>由此可见，<code>Thread-0</code>和<code>Thread-1</code>两个线程输出的<code>i</code>变量不连续，这是因为<code>i</code>变量是<code>FirstThread</code>的实例变量而不是局部变量，而<code>Thread-0</code>和<code>Thread-1</code>是两个对象，所以不能共享实例变量。</p>
<h2 id="实现Runnable接口创建线程"><a href="#实现Runnable接口创建线程" class="headerlink" title="实现Runnable接口创建线程"></a>实现<code>Runnable</code>接口创建线程</h2><p>实现<code>Runnable</code>接口来创建并启动多线程的步骤：</p>
<ul>
<li><ol>
<li>定义<code>Runnable</code>接口的实现类，并重写该接口的<code>run()</code>方法</li>
</ol>
</li>
<li><ol start="2">
<li>创建<code>Runnable</code>实现类的实例，并以此作为<code>Thread</code>的<code>target</code>来创建<code>Thread</code>对象，该<code>Thread</code>才是真正的线程对象。代码如下所示：</li>
</ol>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//创建Runnable实现类的对象</span></span><br><span class="line">SecondThread = <span class="keyword">new</span> SecondThread();</span><br><span class="line"><span class="comment">//以Runnable实现类的对象作为Thread的target来创建Thread对象，即线程对象</span></span><br><span class="line"><span class="keyword">new</span> Thread(st);</span><br></pre></td></tr></table></figure>

<p>也可以在创建<code>Thread</code>对象时指定一个名字：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">new</span> Thread(st,<span class="string">"新线程"</span>);</span><br></pre></td></tr></table></figure>

<ul>
<li><ol start="3">
<li>调用线程对象的<code>start()</code>方法来启动线程</li>
</ol>
</li>
</ul>
<p>示例如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SecondThread</span> <span class="keyword">implements</span> <span class="title">Runnable</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> i;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (; i &lt; <span class="number">100</span>; i++) &#123;</span><br><span class="line">            <span class="comment">// 当线程类实现Runnable()接口时，只能用Thread.currentThread()方法获取当前线程</span></span><br><span class="line">            System.out.println(Thread.currentThread().getName() + <span class="string">" "</span> + i);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">100</span>; i++) &#123;</span><br><span class="line">            System.out.println(Thread.currentThread().getName() + <span class="string">" "</span> + i);</span><br><span class="line">            <span class="keyword">if</span> (i == <span class="number">20</span>) &#123;</span><br><span class="line">                SecondThread st = <span class="keyword">new</span> SecondThread();</span><br><span class="line">                <span class="comment">// 通过new Thread(target,name)方法创建新线程</span></span><br><span class="line">                <span class="keyword">new</span> Thread(st, <span class="string">"新线程1"</span>).start();</span><br><span class="line">                <span class="keyword">new</span> Thread(st, <span class="string">"新线程2"</span>).start();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>与<code>FirstThread</code>中的<code>run()</code>方法体相比，通过继承<code>Thread</code>类获得线程对象比较简单；但实现<code>Runnable</code>接口获得线程对象则必须使用<code>Thread.currentThread()</code>方法</p>
<blockquote>
<p>main 25</p>
<p>新线程1 0</p>
<p>main 26</p>
<p>新线程1 1</p>
<p>main 27</p>
<p>新线程2 2</p>
<p>新线程1 2</p>
<p>新线程2 3</p>
<p>main 28</p>
<p>新线程2 5</p>
<p>新线程1 4</p>
<p>新线程2 6</p>
<p>main 29</p>
</blockquote>
<p>如上所示，两个子线程的<code>i</code>变量基本是连续的（主要因为<code>i++</code>操作非原子性造成不连续），也就是采用<code>Runnable</code>接口方式创建的多个线程可以共享线程类的实例变量。因为在此种方式下，程序所创建的<code>Runnable</code>对象只是线程的<code>target</code>，而多个线程可以共享一个<code>target</code>，故多个线程可以共享有一个线程类（实际上应该是线程的<code>target</code>类）的实例变量。</p>
<h3 id="两种方式的优缺点"><a href="#两种方式的优缺点" class="headerlink" title="两种方式的优缺点"></a>两种方式的优缺点</h3><p>实现<code>Runnable</code>的优缺点</p>
<ul>
<li>线程类只是实现了<code>Runnable</code>接口，还可以继承其他类。</li>
<li>在这种方式下，多个线程可以共享一个<code>target</code>对象，非常适合多个相同线程处理同一份资源</li>
<li>劣势是，编程稍稍复杂，如果需要访问当前线程，必须使用<code>Thread.currentThread()</code>方法<br>继承<code>Thread</code>类创建多线程的优缺点</li>
<li>劣势是，因为已经继承了<code>Thread</code>类，使用不能继承其他父类</li>
<li>优势是编写简单，访问当前线程时只需要使用`this</li>
</ul>

    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/04/22/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day10/" rel="prev" title="Java日拱一卒-day10">
      <i class="fa fa-chevron-left"></i> Java日拱一卒-day10
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/04/29/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day12/" rel="next" title="Java日拱一卒-day12">
      Java日拱一卒-day12 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#线程简介"><span class="nav-number">1.</span> <span class="nav-text">线程简介</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#线程创建"><span class="nav-number">2.</span> <span class="nav-text">线程创建</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#继承Thread类创建线程"><span class="nav-number">3.</span> <span class="nav-text">继承Thread类创建线程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#实现Runnable接口创建线程"><span class="nav-number">4.</span> <span class="nav-text">实现Runnable接口创建线程</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#两种方式的优缺点"><span class="nav-number">4.1.</span> <span class="nav-text">两种方式的优缺点</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="文予"
      src="/uploads/wenyu.png">
  <p class="site-author-name" itemprop="name">文予</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">29</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">文予</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>


        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>











<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'forest',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


  

  

</body>
</html>
